package com.hack4jp.hackearthquakes;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class DetectableSoftKeyLayout extends LinearLayout {

	private OnSoftKeyShownListener _listener = null;
	
	public DetectableSoftKeyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}	

	public interface OnSoftKeyShownListener {
		public void onSoftKeyShown(boolean isShown);
	}
	
	public void setListener(OnSoftKeyShownListener listener) {
		this._listener = listener;
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int SOFTKEY_HEIGHT = 100;

		Activity activity = (Activity)getContext();
		Rect rect = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
		
		int visibleHeight = rect.bottom - rect.top; 
		int displayHeight = activity.getWindowManager().getDefaultDisplay().getHeight(); 
		int diff = displayHeight - visibleHeight; 
		if (_listener != null) {
			_listener.onSoftKeyShown(diff > SOFTKEY_HEIGHT);
		}
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}
